/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.system.api.impl;

import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.PasswordUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.serv.system.api.SysNoApi;
import com.jnzc.ocr.serv.system.api.SysUserApi;
import com.jnzc.ocr.serv.system.consts.SysUserConsts;
import com.jnzc.ocr.serv.system.entity.SysUser;
import com.jnzc.ocr.serv.system.service.SysNoService;
import com.jnzc.ocr.serv.system.service.SysUserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangcm
 * @version 1.0, 2017/8/26
 */
@RestController
public class SysUserApiImpl implements SysUserApi {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysNoService sysNoService;

    @Override
    public SysUser findLevelOneByUsername(String username, Integer type) {
        return sysUserService.findLevelOneByUsername(username, type);
    }

    public SysUser createShopUser(@RequestBody SysUser param) {
        ValidateUtils.isId(param.getCompanyId(), "公司ID");
        ValidateUtils.notEmpty(param.getUsername(), "用户名");
        setCreateDefault(param, SysUserConsts.TYPE_SHOP);
        // 检测一级用户是否重复
        if(SysUserConsts.LEVEL_ONE == param.getLevel()){
            SysUser old = sysUserService.findLevelOneByUsername(param.getUsername(), param.getType());
            BusinessUtils.isNull(old, "用户已存在");
        }
        // 检查用户名在公司用户中是否重复
        SysUser old = sysUserService.findByUsername(param.getUsername(), param.getCompanyId());
        BusinessUtils.isNull(old, "用户已存在");
        // 获取编号
        String no = sysNoService.getNextUserNo(param.getCompanyId());
        param.setNo(no);
        return sysUserService.insert(param);
    }

    public void updatePassword(Long id, String newPassword) {
        ValidateUtils.isPassword(newPassword, "新密码");
        SysUser user = sysUserService.find(id, null);
        String encryptStr = PasswordUtils.encrypt(newPassword, user.getSalt());
        user.setPassword(encryptStr);
        sysUserService.update(user);
    }

    private void setCreateDefault(SysUser param, Integer type){
        param.setType(type);
        if(null == param.getLevel()){
            param.setLevel(SysUserConsts.LEVEL_ONE);
        }
        if(null == param.getIfAdmin()){
            param.setIfAdmin(false);
        }
        if(null == param.getState()){
            param.setState(SysUserConsts.STATE_NORMAL);
        }
        String salt = RandomStringUtils.randomAlphanumeric(10);
        param.setSalt(salt);
        if (StringUtils.isNotEmpty(param.getPassword())) {
            String encryptStr = PasswordUtils.encrypt(param.getPassword(), param.getSalt());
            param.setPassword(encryptStr);
        }
    }
}
